

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=light>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/set/B.svg">
  <link rel="icon" href="/img/set/B.svg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="程守斌">
  <meta name="keywords" content="">
  
    <meta name="description" content="iOS 常用的开发架构在iOS开发中，常用的开发架构有MVC、MVVM和VIPER。下面将分别对这三种架构进行讲解。 1. MVC架构MVC是Model-View-Controller的缩写，是一种将应用程序分成三个核心部件的设计模式。  Model：数据模型，封装了应用程序的数据以及操作数据的方法。 View：用户界面，展示数据以及与用户交互的界面。 Controller：控制器，协调Model">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS 常用的开发架构">
<meta property="og:url" content="https://icoobin.github.io/2023/02/25/ios/iOS-%E5%B8%B8%E7%94%A8%E7%9A%84%E5%BC%80%E5%8F%91%E6%9E%B6%E6%9E%84/index.html">
<meta property="og:site_name" content="Bin&#39;s blog">
<meta property="og:description" content="iOS 常用的开发架构在iOS开发中，常用的开发架构有MVC、MVVM和VIPER。下面将分别对这三种架构进行讲解。 1. MVC架构MVC是Model-View-Controller的缩写，是一种将应用程序分成三个核心部件的设计模式。  Model：数据模型，封装了应用程序的数据以及操作数据的方法。 View：用户界面，展示数据以及与用户交互的界面。 Controller：控制器，协调Model">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-02-25T07:34:16.000Z">
<meta property="article:modified_time" content="2023-02-25T07:37:33.686Z">
<meta property="article:author" content="程守斌">
<meta property="article:tag" content="iOS">
<meta property="article:tag" content="设计模式">
<meta property="article:tag" content="架构">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>iOS 常用的开发架构 - Bin&#39;s blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"icoobin.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"|","loop":false,"scope":["home","post"]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/set/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"0424c6a81239810100c98bd470a50118","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?0424c6a81239810100c98bd470a50118";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 38.2vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Bin&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/banner/posts.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="iOS 常用的开发架构"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-02-25 15:34" pubdate>
          2023年2月25日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          34 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">iOS 常用的开发架构</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="iOS-常用的开发架构"><a href="#iOS-常用的开发架构" class="headerlink" title="iOS 常用的开发架构"></a>iOS 常用的开发架构</h1><p>在iOS开发中，常用的开发架构有MVC、MVVM和VIPER。下面将分别对这三种架构进行讲解。</p>
<h2 id="1-MVC架构"><a href="#1-MVC架构" class="headerlink" title="1. MVC架构"></a>1. MVC架构</h2><p>MVC是Model-View-Controller的缩写，是一种将应用程序分成三个核心部件的设计模式。</p>
<ul>
<li>Model：数据模型，封装了应用程序的数据以及操作数据的方法。</li>
<li>View：用户界面，展示数据以及与用户交互的界面。</li>
<li>Controller：控制器，协调Model和View之间的通信。</li>
</ul>
<h3 id="MVC-Swift-5-示例代码"><a href="#MVC-Swift-5-示例代码" class="headerlink" title="MVC Swift 5 示例代码"></a>MVC Swift 5 示例代码</h3><div class="code-wrapper"><pre><code class="hljs swift"><span class="hljs-comment">// Model</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Person</span> &#123;
    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">var</span> age: <span class="hljs-type">Int</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">name</span>: <span class="hljs-type">String</span>, <span class="hljs-params">age</span>: <span class="hljs-type">Int</span>) &#123;
        <span class="hljs-keyword">self</span>.name <span class="hljs-operator">=</span> name
        <span class="hljs-keyword">self</span>.age <span class="hljs-operator">=</span> age
    &#125;
&#125;

<span class="hljs-comment">// View</span>
<span class="hljs-keyword">import</span> UIKit

<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonView</span>: <span class="hljs-title class_">UIView</span> &#123;
    <span class="hljs-keyword">let</span> nameLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()
    <span class="hljs-keyword">let</span> ageLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()

    <span class="hljs-keyword">override</span> <span class="hljs-keyword">init</span>(<span class="hljs-params">frame</span>: <span class="hljs-type">CGRect</span>) &#123;
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(frame: frame)
        nameLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        ageLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">60</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        <span class="hljs-keyword">self</span>.addSubview(nameLabel)
        <span class="hljs-keyword">self</span>.addSubview(ageLabel)
    &#125;

    <span class="hljs-keyword">required</span> <span class="hljs-keyword">init?</span>(<span class="hljs-params">coder</span>: <span class="hljs-type">NSCoder</span>) &#123;
        <span class="hljs-built_in">fatalError</span>(<span class="hljs-string">&quot;init(coder:) has not been implemented&quot;</span>)
    &#125;

    <span class="hljs-keyword">func</span> <span class="hljs-title function_">configure</span>(<span class="hljs-params">with</span> <span class="hljs-params">person</span>: <span class="hljs-type">Person</span>) &#123;
        nameLabel.text <span class="hljs-operator">=</span> person.name
        ageLabel.text <span class="hljs-operator">=</span> <span class="hljs-string">&quot;<span class="hljs-subst">\\</span>(person.age)&quot;</span>
    &#125;
&#125;

<span class="hljs-comment">// Controller</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonController</span> &#123;
    <span class="hljs-keyword">let</span> person: <span class="hljs-type">Person</span>
    <span class="hljs-keyword">let</span> personView: <span class="hljs-type">PersonView</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">person</span>: <span class="hljs-type">Person</span>, <span class="hljs-params">personView</span>: <span class="hljs-type">PersonView</span>) &#123;
        <span class="hljs-keyword">self</span>.person <span class="hljs-operator">=</span> person
        <span class="hljs-keyword">self</span>.personView <span class="hljs-operator">=</span> personView
        <span class="hljs-keyword">self</span>.personView.configure(with: person)
    &#125;
&#125;
</code></pre></div>

<h2 id="2-MVVM架构"><a href="#2-MVVM架构" class="headerlink" title="2. MVVM架构"></a>2. MVVM架构</h2><p>MVVM是Model-View-ViewModel的缩写，是在MVC架构的基础上发展而来的一种设计模式。</p>
<ul>
<li>Model：数据模型，封装了应用程序的数据以及操作数据的方法。</li>
<li>View：用户界面，展示数据以及与用户交互的界面。</li>
<li>ViewModel：作为View和Model之间的中间层，负责处理View的展示逻辑，将Model的数据转化为View需要的数据。</li>
</ul>
<h3 id="MVVM-Swift-5-示例代码"><a href="#MVVM-Swift-5-示例代码" class="headerlink" title="MVVM Swift 5 示例代码"></a>MVVM Swift 5 示例代码</h3><div class="code-wrapper"><pre><code class="hljs swift"><span class="hljs-comment">// Model</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Person</span> &#123;
    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">var</span> age: <span class="hljs-type">Int</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">name</span>: <span class="hljs-type">String</span>, <span class="hljs-params">age</span>: <span class="hljs-type">Int</span>) &#123;
        <span class="hljs-keyword">self</span>.name <span class="hljs-operator">=</span> name
        <span class="hljs-keyword">self</span>.age <span class="hljs-operator">=</span> age
    &#125;
&#125;

<span class="hljs-comment">// ViewModel</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonViewModel</span> &#123;
    <span class="hljs-keyword">var</span> person: <span class="hljs-type">Person</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">person</span>: <span class="hljs-type">Person</span>) &#123;
        <span class="hljs-keyword">self</span>.person <span class="hljs-operator">=</span> person
    &#125;

    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span> &#123;
        <span class="hljs-keyword">return</span> person.name
    &#125;

    <span class="hljs-keyword">var</span> age: <span class="hljs-type">String</span> &#123;
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;<span class="hljs-subst">\\</span>(person.age)&quot;</span>
    &#125;
&#125;

<span class="hljs-comment">// View</span>
<span class="hljs-keyword">import</span> UIKit

<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonView</span>: <span class="hljs-title class_">UIView</span> &#123;
    <span class="hljs-keyword">let</span> nameLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()
    <span class="hljs-keyword">let</span> ageLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()
    <span class="hljs-keyword">var</span> viewModel: <span class="hljs-type">PersonViewModel</span>? &#123;
        <span class="hljs-keyword">didSet</span> &#123;
            nameLabel.text <span class="hljs-operator">=</span> viewModel<span class="hljs-operator">?</span>.name
            ageLabel.text <span class="hljs-operator">=</span> viewModel<span class="hljs-operator">?</span>.age
        &#125;
    &#125;

    <span class="hljs-keyword">override</span> <span class="hljs-keyword">init</span>(<span class="hljs-params">frame</span>: <span class="hljs-type">CGRect</span>) &#123;
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(frame: frame)
        nameLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        ageLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">60</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        <span class="hljs-keyword">self</span>.addSubview(nameLabel)
        <span class="hljs-keyword">self</span>.addSubview(ageLabel)
    &#125;

    <span class="hljs-keyword">required</span> <span class="hljs-keyword">init?</span>(<span class="hljs-params">coder</span>: <span class="hljs-type">NSCoder</span>) &#123;
        <span class="hljs-built_in">fatalError</span>(<span class="hljs-string">&quot;init(coder:) has not been implemented&quot;</span>)
    &#125;
&#125;

<span class="hljs-comment">// Controller</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonController</span> &#123;
    <span class="hljs-keyword">let</span> personView: <span class="hljs-type">PersonView</span>
    <span class="hljs-keyword">let</span> personViewModel: <span class="hljs-type">PersonViewModel</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">person</span>: <span class="hljs-type">Person</span>) &#123;
        <span class="hljs-keyword">self</span>.personViewModel <span class="hljs-operator">=</span> <span class="hljs-type">PersonViewModel</span>(person: person)
        <span class="hljs-keyword">self</span>.personView <span class="hljs-operator">=</span> <span class="hljs-type">PersonView</span>()
        <span class="hljs-keyword">self</span>.personView.viewModel <span class="hljs-operator">=</span> personViewModel
    &#125;
&#125;
</code></pre></div>

<h2 id="3-VIPER架构"><a href="#3-VIPER架构" class="headerlink" title="3. VIPER架构"></a>3. VIPER架构</h2><p>VIPER是View-Interactor-Presenter-Entity-Router的缩写，是一种通过将应用程序的逻辑分解成多个独立的模块来构建清晰、可扩展和易于维护的架构。</p>
<ul>
<li>View：用户界面，展示数据以及与用户交互的界面。</li>
<li>Interactor：业务逻辑层，负责处理业务逻辑。</li>
<li>Presenter：负责将Interactor返回的数据转化为View需要的数据。</li>
<li>Entity：数据模型，封装了应用程序的数据以及操作数据的方法。</li>
<li>Router：负责处理模块之间的跳转逻辑。</li>
</ul>
<h3 id="VIPER-Swift-5-示例代码"><a href="#VIPER-Swift-5-示例代码" class="headerlink" title="VIPER Swift 5 示例代码"></a>VIPER Swift 5 示例代码</h3><div class="code-wrapper"><pre><code class="hljs swift"><span class="hljs-comment">// Entity</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Person</span> &#123;
    <span class="hljs-keyword">var</span> name: <span class="hljs-type">String</span>
    <span class="hljs-keyword">var</span> age: <span class="hljs-type">Int</span>

    <span class="hljs-keyword">init</span>(<span class="hljs-params">name</span>: <span class="hljs-type">String</span>, <span class="hljs-params">age</span>: <span class="hljs-type">Int</span>) &#123;
        <span class="hljs-keyword">self</span>.name <span class="hljs-operator">=</span> name
        <span class="hljs-keyword">self</span>.age <span class="hljs-operator">=</span> age
    &#125;
&#125;

<span class="hljs-comment">// Interactor</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonInteractor</span> &#123;
    <span class="hljs-keyword">var</span> person: <span class="hljs-type">Person</span>?

    <span class="hljs-keyword">func</span> <span class="hljs-title function_">fetchPerson</span>() -&gt; <span class="hljs-type">Person</span> &#123;
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> person <span class="hljs-operator">=</span> person &#123;
            <span class="hljs-keyword">return</span> person
        &#125; <span class="hljs-keyword">else</span> &#123;
            <span class="hljs-keyword">let</span> person <span class="hljs-operator">=</span> <span class="hljs-type">Person</span>(name: <span class="hljs-string">&quot;Tom&quot;</span>, age: <span class="hljs-number">18</span>)
            <span class="hljs-keyword">self</span>.person <span class="hljs-operator">=</span> person
            <span class="hljs-keyword">return</span> person
        &#125;
    &#125;
&#125;

<span class="hljs-comment">// Presenter</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonPresenter</span> &#123;
    <span class="hljs-keyword">var</span> person: <span class="hljs-type">Person</span>?
    <span class="hljs-keyword">weak</span> <span class="hljs-keyword">var</span> view: <span class="hljs-type">PersonView</span>?
    <span class="hljs-keyword">let</span> interactor <span class="hljs-operator">=</span> <span class="hljs-type">PersonInteractor</span>()

    <span class="hljs-keyword">func</span> <span class="hljs-title function_">fetchPerson</span>() &#123;
        <span class="hljs-keyword">let</span> person <span class="hljs-operator">=</span> interactor.fetchPerson()
        <span class="hljs-keyword">self</span>.person <span class="hljs-operator">=</span> person
        view<span class="hljs-operator">?</span>.configure(with: person)
    &#125;
&#125;

<span class="hljs-comment">// View</span>
<span class="hljs-keyword">import</span> UIKit

<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonView</span>: <span class="hljs-title class_">UIView</span> &#123;
    <span class="hljs-keyword">let</span> nameLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()
    <span class="hljs-keyword">let</span> ageLabel <span class="hljs-operator">=</span> <span class="hljs-type">UILabel</span>()
    <span class="hljs-keyword">var</span> presenter: <span class="hljs-type">PersonPresenter</span>?

    <span class="hljs-keyword">override</span> <span class="hljs-keyword">init</span>(<span class="hljs-params">frame</span>: <span class="hljs-type">CGRect</span>) &#123;
        <span class="hljs-keyword">super</span>.<span class="hljs-keyword">init</span>(frame: frame)
        nameLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        ageLabel.frame <span class="hljs-operator">=</span> <span class="hljs-type">CGRect</span>(x: <span class="hljs-number">0</span>, y: <span class="hljs-number">60</span>, width: <span class="hljs-number">100</span>, height: <span class="hljs-number">50</span>)
        <span class="hljs-keyword">self</span>.addSubview(nameLabel)
        <span class="hljs-keyword">self</span>.addSubview(ageLabel)
    &#125;

    <span class="hljs-keyword">required</span> <span class="hljs-keyword">init?</span>(<span class="hljs-params">coder</span>: <span class="hljs-type">NSCoder</span>) &#123;
        <span class="hljs-built_in">fatalError</span>(<span class="hljs-string">&quot;init(coder:) has not been implemented&quot;</span>)
    &#125;

    <span class="hljs-keyword">func</span> <span class="hljs-title function_">configure</span>(<span class="hljs-params">with</span> <span class="hljs-params">person</span>: <span class="hljs-type">Person</span>) &#123;
        nameLabel.text <span class="hljs-operator">=</span> person.name
        ageLabel.text <span class="hljs-operator">=</span> <span class="hljs-string">&quot;<span class="hljs-subst">\\</span>(person.age)&quot;</span>
    &#125;
&#125;

<span class="hljs-comment">// Router</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonRouter</span> &#123;

&#125;

<span class="hljs-comment">// Controller</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">PersonController</span> &#123;
    <span class="hljs-keyword">let</span> personView <span class="hljs-operator">=</span> <span class="hljs-type">PersonView</span>()
    <span class="hljs-keyword">let</span> personPresenter <span class="hljs-operator">=</span> <span class="hljs-type">PersonPresenter</span>()
    <span class="hljs-keyword">let</span> personRouter <span class="hljs-operator">=</span> <span class="hljs-type">PersonRouter</span>()

    <span class="hljs-keyword">init</span>() &#123;
        personPresenter.view <span class="hljs-operator">=</span> personView
        personView.presenter <span class="hljs-operator">=</span> personPresenter
    &#125;
&#125;</code></pre></div>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/iOS/" class="category-chain-item">iOS</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/iOS/">#iOS</a>
      
        <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">#设计模式</a>
      
        <a href="/tags/%E6%9E%B6%E6%9E%84/">#架构</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>iOS 常用的开发架构</div>
      <div>https://icoobin.github.io/2023/02/25/ios/iOS-常用的开发架构/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>程守斌</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年2月25日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/02/26/other/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/" title="排序算法">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">排序算法</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/02/25/other/24-%E7%A7%8D%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-1-Swift-5%E7%89%88/" title="24 种设计模式-1 (Swift 5版)">
                        <span class="hidden-mobile">24 种设计模式-1 (Swift 5版)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
      <div class="col-lg-7 mx-auto nopadding-x-md">
        <div class="container custom mx-auto">
           <script src="/js/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script> <script>L2Dwidget.init({ pluginRootPath: "live2dw/", pluginJsPath: "lib/", pluginModelPath: "assets/", tagMode: !1, log: !1, model: { jsonPath: "/js/live2dw/assets/hijiki.model.json" }, display: { position: "left", hOffset: 60, vOffset: 0, width: 150, height: 300 }, mobile: { show: !1 } })</script> 
        </div>
      </div>
    
  </main>

  <footer>
    <div class="footer-inner">
    
        <div class="footer-content">
             <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
        </div>
        
            
                <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

                    
                        
                                    
</div>

<!-- <script src="js/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script>
<script>L2Dwidget.init({ pluginRootPath: "live2dw/", pluginJsPath: "lib/", pluginModelPath: "assets/", tagMode: !1, log: !1, model: { jsonPath: "js/live2dw/assets/hijiki.model.json" }, display: { position: "left", hOffset: 60, vOffset: 0, width: 150, height: 300 }, mobile: { show: !1 } })</script> -->

<!--
<div id="music_div" style="position: fixed; bottom: 0px; left: 30px;">
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=110
        src="//music.163.com/outchain/player?type=0&id=919444742&auto=1&height=90">
    </iframe>
    <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script>
    <script src="/js/musicshow.js"></script>
</div>
-->

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
